package com.bachelor.community.controller;

import cn.hutool.core.date.DateUtil;
import com.bachelor.community.annotation.UnInterception;
import com.bachelor.community.model.entity.Comment;
import com.bachelor.community.service.intf.CommentService;
import com.bachelor.community.utils.Result;
import com.bachelor.community.utils.UserSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("answers/{answerId}")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private UserSupport userSupport;

    @PostMapping("comments")
    public Result insertComment(@PathVariable Integer answerId, @RequestBody Comment comment) {
        Integer commentator = userSupport.getCurrentUserId();
        comment.setCommentator(commentator);
        comment.setCreateTime(DateUtil.date().toTimestamp());
        return Result.succeed("201", commentService.save(comment));
    }

    @GetMapping("comments")
    @UnInterception
    public Result listComments(@PathVariable Integer answerId) {
        return Result.succeed("200", commentService.listComments(answerId));
    }
}

